package com.sicong.admin.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sicong.admin.entity.Role;
import com.sicong.admin.entity.RolePermission;
import com.sicong.admin.service.RolePermissionService;
import com.sicong.admin.service.RoleService;
import com.sicong.common.core.utils.Result;
import com.sicong.common.core.utils.ResultUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author chenww
 * @since 2020-11-25
 */
@Api(tags = {"角色控制器"})
@RestController
@RequestMapping("/role")
public class RoleController {

    @Resource
    private RoleService roleService;

    @Resource
    private RolePermissionService rolePermissionService;

    @ApiOperation("添加角色")
    @PostMapping("/save")
    public Result<Void> saveRole(@ApiParam("角色名称") @RequestParam("roleName") String roleName,
                                 @ApiParam("角色编码") @RequestParam("roleCode") String roleCode,
                                 @ApiParam("备注") @RequestParam(value = "remark",required = false) String remark) {
        Role role = new Role()
                .setRoleName(roleName)
                .setRoleCode(roleCode)
                .setRemark(remark);
        roleService.save(role);
        return ResultUtil.success();
    }

    @ApiOperation("删除角色")
    @DeleteMapping("/{roleIds}")
    public Result<Void> removeRoles(@ApiParam("角色id") @PathVariable String roleIds) {
        roleService.removeByIds(Arrays.asList(roleIds.split(",")));
        return ResultUtil.success();
    }

    @ApiOperation("修改角色信息")
    @PatchMapping("/{id}")
    public Result<Void> updateRole(@ApiParam("角色id") @PathVariable Long id,
                                   @ApiParam("角色名称") @RequestParam(value = "roleName", required = false) String roleName,
                                   @ApiParam("角色编码") @RequestParam(value = "roleCode", required = false) String roleCode,
                                   @ApiParam("备注") @RequestParam(value = "remark", required = false) String remark) {
        Role role = new Role()
                .setId(id)
                .setRoleName(roleName)
                .setRoleCode(roleCode)
                .setRemark(remark);
        roleService.updateById(role);
        return ResultUtil.success();
    }

    @ApiOperation("分页查询角色")
    @GetMapping("/page")
    public Result<IPage<Role>> getRoles(Page<Role> page,
                                        @ApiParam("角色名称") @RequestParam(value = "roleName", required = false) String roleName,
                                        @ApiParam("角色编码") @RequestParam(value = "roleCode", required = false) String roleCode) {
        return ResultUtil.success(
                roleService.page(page, Wrappers.<Role>lambdaQuery()
                        .select(Role::getId, Role::getRoleName, Role::getRoleCode, Role::getRemark)
                        .eq(!StringUtils.isEmpty(roleName), Role::getRoleName, roleName)
                        .eq(!StringUtils.isEmpty(roleCode), Role::getRoleCode, roleCode))
        );
    }

    @ApiOperation("添加权限")
    @PostMapping("/addPermissions/{roleId}")
    public Result<Void> addPermissions(@ApiParam("角色id") @PathVariable Long roleId,
                                       @ApiParam("权限id") @RequestParam("permissionIds") String ids) {
        String[] permissionIds = ids.split(",");
        List<RolePermission> rolePermissions = new ArrayList<>();
        for (String permissionId : permissionIds) {
            rolePermissions.add(new RolePermission().setRoleId(roleId).setPermissionId(Long.valueOf(permissionId)));
        }
        rolePermissionService.saveBatch(rolePermissions);
        return ResultUtil.success();
    }

    @ApiOperation("分配权限")
    @PostMapping("/modifyPermissions/{roleId}")
    public Result<Void> modifyPermissions(@ApiParam("角色id") @PathVariable Long roleId,
                                          @ApiParam("权限id") @RequestParam("permissionIds") String ids) {
        rolePermissionService.remove(Wrappers.<RolePermission>lambdaQuery().eq(RolePermission::getRoleId,roleId));
        List<Long> permissionIds = Arrays.stream(ids.split(",")).map(Long::valueOf).collect(Collectors.toList());
        List<RolePermission> permissions = new ArrayList<>();
        for (Long permissionId : permissionIds) {
            permissions.add(new RolePermission().setRoleId(roleId).setPermissionId(permissionId));
        }
        rolePermissionService.saveBatch(permissions);
        return  ResultUtil.success();
    }

}

